package pers.xuehongyan.useat.server.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import pers.xuehongyan.useat.server.auth.JwtUserUtils;
import pers.xuehongyan.useat.server.enity.User;
import pers.xuehongyan.useat.server.exception.user.UserNotFoundException;
import pers.xuehongyan.useat.server.mapper.UserMapper;

import javax.annotation.Resource;

/**
 * @author xuehongyan
 */
@Component
public class UserManager {
    @Resource
    UserMapper userMapper;

    @Resource
    JwtUserUtils jwtUserUtils;

    public void create(User user) {
        userMapper.insert(user);
    }

    public User getUserByName(String name) throws UserNotFoundException {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("name", name));
        if(user == null) {
            throw new UserNotFoundException(name);
        }
        return user;
    }

    public User getUserByToken(String token) throws UserNotFoundException {
        return jwtUserUtils.getUserByToken(token);
    }

    public String getTokenByUser(User user) {
        return jwtUserUtils.getTokenByUser(user);
    }

    public void update(User user) {
        userMapper.updateById(user);
    }
}
